package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

public interface AddressMapper {
    /**
     * 查询所有地址
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> list(Long userId);

    /**
     * 查询默认地址
     * @param currentId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook one(Long currentId);

    /**
     * 新增地址
     * @param addressBook
     */
    @Insert("insert into address_book value (null,#{userId},#{consignee},#{sex},#{phone},#{provinceCode},#{provinceName},#{cityCode},#{cityName},#{districtCode},#{districtName},#{detail},#{label},#{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    @Cacheable(cacheNames = "addressBook",key = "#id")
    AddressBook getById(Long id);
    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @Delete("delete from address_book where id = #{id}")
    @CacheEvict(value = "addressBook", key = "#id")
    void deleteById(Long id);

    /**
     * 修改地址
     * @param addressBook
     */
    @CacheEvict(value = "addressBook", key = "#addressBook.id")
    void update(AddressBook addressBook);
}
